package com.itheima.web.controller;

import com.itheima.pojo.IdIntegral;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import com.itheima.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PreDestroy;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/v1/user")
public class UserContoller {
    @Autowired
    private UserService userService;
    @GetMapping("/findByClassId")
    public ResponseResult findUser(HttpServletRequest request){
        Integer classId = (Integer) request.getSession().getAttribute("ClassId");
        List<User> users = userService.findByClassId(classId);
        if (users != null){
            return new ResponseResult<>(0,"操作成功",users);
        }
        return new ResponseResult<>(1,"操作失败",null);
    }
    @PostMapping("addIntegral")
    public ResponseResult addIntegral(@RequestBody IdIntegral idIntegral){
        boolean flag = userService.updateById(idIntegral);
        if (flag){
        return new ResponseResult<>(0,"操作成功",null);
        }
        return new ResponseResult<>(1,"操作失败",null);
    }
    @GetMapping("resetGScoreByGroupId/{classId}")
    public ResponseResult updateIntegralByClassId(@PathVariable Integer classId){
       return userService.updateIntegralByClassId(classId);
    }

}